package com.itheima12;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author Xiao
 * @date 2021/7/27
 */
public class StudentTest {
    /**
     * 需求：ArrayList存储5个学生对象，使用Collections对ArrayList进行排序。
     * 要求：按照年龄从小到大排序，年龄相同时，按照姓名的字母顺序排序。
     */
    public static void main(String[] args) {
        Student stu1 = new Student("aliang", 24);
        Student stu2 = new Student("zuoyou", 24);
        Student stu3 = new Student("pingan", 21);
        Student stu4 = new Student("dafeng", 28);
        Student stu5 = new Student("baoping", 16);
        ArrayList<Student> list = new ArrayList<>(List.of(stu1, stu2, stu3, stu4, stu5));
        Collections.sort(list, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                int result = o1.getAge() - o2.getAge();
                return result == 0 ? o1.getName().compareTo(o2.getName()) : result;
            }
        });
        for (Student stu : list) {
            System.out.println(stu);
        }
    }
}
